var FreeboxLinkDownloadUI = function(element) {
	
	var block;
	
	var downloadSpeed;
	var uploadSpeed;
	var blockSpeed;
	
	var downloadInfo;
	var progressText;
	
	var progressbar;
	
	var filename;
	
	var linkPause;
	var linkResume;
	var linkCancel;
	
	this.onpause = function() { };
	this.onresume = function() { };
	this.oncancel = function() { };
	
	var fillingObject;
	
	var $this = this;
	
	var Tools = ToolsPrototypeJS;
	
	function __construct(element) {
		initUI(element);
		initEvent();
	}
	
	function initUI(element) {
		block = element;
		downloadSpeed 			= Tools.findElement(element, ".downloadspeed");
		uploadSpeed 			= Tools.findElement(element, ".uploadspeed");
		blockSpeed				= Tools.findElement(element, ".speed-info-block .speed");
		downloadInfo 			= Tools.findElement(element, ".download-text");
		progressText 			= Tools.findElement(element, ".progressbar .progressbar-text");
		progressbar				= Tools.findElement(element, ".progressbar .progressed");
		filename 				= Tools.findElement(element, ".filename");
		linkPause				= Tools.findElement(element, ".link-pause");
		linkResume 				= Tools.findElement(element, ".link-resume");
		linkCancel 				= Tools.findElement(element, ".link-cancel");
	}
	
	function onpause() {
		var res = $this.onpause(fillingObject, element, "pause");
		if (res == undefined || res) {
			Tools.addClassName(linkPause, "hidden");
			Tools.removeClassName(linkResume, "hidden");
			Tools.addClassName(blockSpeed, "hidden");
		}
	}
	
	function onresume() {
		var res = $this.onresume(fillingObject, element, "resume");		
		if (res == undefined || res) {
			Tools.addClassName(linkResume, "hidden");
			Tools.removeClassName(linkPause, "hidden");
			Tools.removeClassName(blockSpeed, "hidden");
		}
	}
	
	function oncancel() {		
		$this.oncancel(fillingObject, element, "cancel");		
	}
	
	function initEvent() {
		Tools.on("click", linkPause, onpause);
		Tools.on("click", linkResume, onresume);
		Tools.on("click", linkCancel, oncancel);
	}
	
	this.setFillingObject = function(obj) {
		initFromFreeboxV6Download(obj);
		fillingObject = obj;
	}

	function setTextOnElement(element, text) {
		if (element) {
			element.innerHTML = text;
		}
	}
	
	this.getElement = function() {
		return block;
	}
	
	this.setFilename = function(text) {
		setTextOnElement(filename, text);
	}
	
	this.setDownloadInfo = function(text) {
		setTextOnElement(downloadInfo, text);
	}
	
	this.setProgressText = function(text) {
		setTextOnElement(progressText, text);
	}
	
	this.setProgress = function(percent) {
		Tools.css(progressbar, "width", percent+"%");
	}
	
	this.setDownloadSpeed = function(text) {
		setTextOnElement(downloadSpeed, text);
	}
	
	this.setUploadSpeed = function(text) {
		setTextOnElement(uploadSpeed, text);
	}
	
	function getAppropriateUnit(i) {
		if (i >= (1024*1024*1024)) {
			unit = "go";
		}
		else if (i >= (1024*1024)) {
			unit = "mo";
		}
		else {
			unit = "ko";
		}
		return unit;
	}
	
	function initFromFreeboxV6Download(obj) {
		var unit = getAppropriateUnit(obj.size);
		var size = obj.getSizeUnit(unit).toFixed(2)+" "+unit;
		var transferred = obj.getTransferredUnit(unit).toFixed(2)+" "+unit;
		
		var percentProgress = (obj.transferred / obj.size).toFixed(4);
		
		var unit = getAppropriateUnit(obj.downloadSpeed);
		var sizeFixed = 2;
		if (unit == "ko") {
			sizeFixed = 0;
		}
		var downloadSpeed = obj.getDownloadSpeedUnit(unit).toFixed(sizeFixed)+" "+unit+" /s";
		
		var unit = getAppropriateUnit(obj.uploadSpeed);
		var sizeFixed = 2;
		if (unit == "ko") {
			sizeFixed = 0;
		}
		var uploadSpeed = obj.getUploadSpeedUnit(unit).toFixed(sizeFixed)+" "+unit+" /s";
		
		var text = "Progression inconnue.";
		var paused = false;
		if (!obj.isFinished() && obj.isRunning()) {
			text = "Téléchargement en cours...";
		}
		else if (obj.isFinished() && !obj.isSeeding()) {
			text = "Téléchargement terminé";
			paused = true;
		}
		else if (obj.isSeeding()) {
			text = "Seeding en cours...";
		}
		else if (obj.isPaused()) {
			text = "Mis en pause";
			paused = true;
		}
		var progressText = (percentProgress*100).toFixed(2) + "%" + " ("+transferred + " / " + size + ")";
		
		$this.setFilename(obj.name);
		$this.setDownloadSpeed(downloadSpeed);
		$this.setUploadSpeed(uploadSpeed);
		$this.setDownloadInfo(text);
		$this.setProgress(percentProgress*100);
		$this.setProgressText(progressText);

		if (paused) {
			Tools.addClassName(linkPause, "hidden");
			Tools.removeClassName(linkResume, "hidden");
			Tools.addClassName(blockSpeed, "hidden");
		}
		else {
			Tools.addClassName(linkResume, "hidden");
			Tools.removeClassName(linkPause, "hidden");
			Tools.removeClassName(blockSpeed, "hidden");
		}
		
	}
	
	__construct(element);
	
};